<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.keywords" placeholder="关键字/id/题目标题" style="width: 200px;" class="filter-item"
        @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getList">搜索</el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-plus" @click="add">添加一个新地区</el-button>
    </div>
    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="ID" prop="id" sortable="custom" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="城市名称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否热门" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.hot >0" style="color: red;">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">
          <div>
            <span v-permission="['/library/answerEdit']">
              <el-button size="small" type="primary" @click="edit(scope.row)">编 辑</el-button>
              <el-button size="small" type="primary" @click="del(scope.row)">删 除</el-button>
            </span>
          </div>
        </template>
      </el-table-column>
    </el-table>


    <el-dialog title="编辑课程" :visible.sync="show">
      <el-form>
        <el-form-item label="地区名称" label-width="150px">
          <el-input v-model="item.name" />
        </el-form-item>
        <el-form-item label="是否热门" label-width="150px">
          <el-select v-model="item.hot" placeholder="是否热门" clearable style="width: 150px" class="filter-item">
            <el-option label="否" :value="0" />
            <el-option label="是" :value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handle()">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import {
    addressDel,
    addressList,
    addressEdit
  } from '@/api/config';
  import waves from '@/directive/waves';
  import Pagination from '@/components/Pagination';
  import permission from '@/directive/permission/index.js';
  import {
    dateHandle
  } from '@/api/dateHandle';


  export default {
    directives: {
      waves,
      permission,

    },
    data() {
      return {
        tableKey: 0,
        list: null,
        listLoading: true,
        listQuery: {},
        show: false,
        item: {}
      };
    },
    created() {
      this.getList()
    },
    methods: {
      _date(t) {
        return dateHandle('Y-m-d H:i:s', t)
      },
      del(item) {
        this.$confirm('确认删除该地区?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          console.log(111);
          addressDel({
            id: item.id
          }).then(res => {
            if (res.code == 20000) {
              this.$message({
                type: 'success',
                message: res.msg
              });
              this.show = false;
              this.getList()
            }
          })
        }).catch(() => {})
      },
      add() {
        this.item = {}
        this.show = true
      },
      edit(item) {
        this.item = item
        this.show = true
      },

      handle() {
        addressEdit(this.item).then(res => {
          if (res.code == 20000) {
            this.$message({
              type: 'success',
              message: res.msg
            });
            this.show = false;
            this.getList()
          }
        })
      },
      getList() {
        this.listLoading = true;
        addressList(this.listQuery).then(res => {
          this.list = res.data;
          setTimeout(() => {
            this.listLoading = false;
          }, 0.3 * 1000);
        });
      },

    }
  };
</script>
